﻿namespace Yom.WebService.Admin
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Web;
    using Yom.Common;
    using Yom.Template;

    public class AdminCategory : AdminBase
    {
        public void OutWrite()
        {
            string ID = Factory.Request("ID");
            string Name = Factory.Request("Name");
            string Save = Factory.Request("Save");
            string Key = Factory.Request("Key");
            string Des = Factory.Request("Des");
            string Action = Factory.Request("Action");
            StringBuilder builder;

            switch (Action)
            {
                case "Add":
                    if (string.IsNullOrEmpty(Name))
                    {
                        builder = new StringBuilder();
                        builder.Append("<fieldset class=\"note\"><dl><dd>分类名称</dd>");
                        builder.Append("<dt><input id=\"name\" type=\"text\" class=\"txt\" /></dt></dl>");
                        builder.Append("<dl><dd>保存目录</dd><dt><input type=\"text\" id=\"save\" class=\"txt\" /></dt></dl>");
                        builder.Append("<dl><dd>Meta 关键字</dd><dt><textarea id=\"key\" cols=\"50\" rows=\"5\"></textarea></dt></dl>");
                        builder.Append("<dl><dd>Meta 描述</dd><dt><textarea id=\"des\" cols=\"50\" rows=\"5\"></textarea></dt></dl>");
                        builder.Append("</fieldset><div id=\"ErrShow\" style=\"color:red\"></div><div class=\"SubButton\"><input type=\"button\" id=\"Submit\" value=\"添加分类\" class=\"button\"/></div>");
                        base.Response.Write(builder.ToString());
                    }
                    else
                    {
                        Yom.Model.Category model = new Yom.Model.Category();
                        model.CategoryName = Name;
                        model.SaveName = Save;
                        model.SeoDes = Des;
                        model.SeoKey = Key;
                        if (new Yom.Data.Category().Add(model))
                        {
                            base.Response.Write("1");
                        }
                        else
                        {
                            base.Response.Write("0");
                        }
                    }
                    break;
                case "Edit":
                    if (!string.IsNullOrEmpty(ID))
                    {
                        if (string.IsNullOrEmpty(Name))
                        {
                            Yom.Model.Category model = new Yom.Data.Category().GetModel(ID.ToInt());
                            builder = new StringBuilder();
                            builder.Append("<fieldset class=\"note\"><dl><dd>分类名称</dd>");
                            builder.Append("<dt><input id=\"name\" type=\"text\" class=\"txt\" value=\"" + model.CategoryName + "\" /></dt></dl>");
                            builder.Append("<dl><dd>保存目录</dd><dt><input type=\"text\" id=\"save\" class=\"txt\" value=\"" + model.SaveName + "\" /></dt></dl>");
                            builder.Append("<dl><dd>Meta 关键字</dd><dt><textarea id=\"key\" cols=\"50\" rows=\"5\">" + model.SeoKey + "</textarea></dt></dl>");
                            builder.Append("<dl><dd>Meta 描述</dd><dt><textarea id=\"des\" cols=\"50\" rows=\"5\">" + model.SeoDes + "</textarea></dt></dl>");
                            builder.Append("</fieldset><div id=\"ErrShow\" style=\"color:red\"></div><div class=\"SubButton\"><input type=\"button\" id=\"Submit\" value=\"修改分类\" class=\"button\"/></div>");
                            base.Response.Write(builder.ToString());
                        }
                        else
                        {
                            Yom.Model.Category model = new Yom.Data.Category().GetModel(ID.ToInt());
                            model.CategoryName = Name;
                            model.SaveName = Save;
                            model.SeoKey = Key;
                            model.SeoDes = Des;
                            if (new Yom.Data.Category().Update(model))
                            {
                                base.Response.Write("1");
                            }
                            else
                            {
                                base.Response.Write("0");
                            }
                        }
                    }
                    break;
                case "Del":
                    if (!string.IsNullOrEmpty(ID))
                    {
                        Yom.Model.Category cmodel = new Yom.Data.Category().GetModel(ID.ToInt());

                        if (new Yom.Data.Category().Delete(ID.ToInt()))
                        {
                            List<Yom.Model.Comic> clist = new Yom.Data.Comic().GetList("SELECT * FROM [Yom_Comic] WHERE [CategoryID]=" + ID);
                            foreach (Yom.Model.Comic model in clist)
                            {
                                try
                                {
                                    string dirPath = System.IO.Path.Combine(Factory.RootPath, "Files/Cartoon/" + model.ComicID.ToString());
                                    Fso.DeleteDirectory(dirPath);
                                }
                                catch (Exception ex) { }
                                new Yom.Data.Comic().Delete(model.ComicID);
                            }
                            try
                            {
                                Fso.DeleteDirectory(Factory.RootPath + "\\" + cmodel.SaveName);
                            }
                            catch (Exception ex) { }
                            base.Response.Write("1");
                        }
                        else
                            base.Response.Write("0");
                    }
                    break;
                case "Make":
                    if (!string.IsNullOrEmpty(ID))
                    {
                        bool makeC = Factory.Request("makeC").ToBool();
                        bool makeV = Factory.Request("makeV").ToBool();
                        try
                        {
                            MakeHtml.MakeCategory(ID.ToInt(), makeC, makeV);
                            base.Response.Write("1");
                        }
                        catch
                        {
                            base.Response.Write("0");
                        }
                    }
                    break;
                default:
                    builder = new StringBuilder();
                    builder.Append("<div class=\"SubMenu\"><a href=\"javascript:$.Run(4)\">分类管理</a> | <a href=\"javascript:$.Run(4,1)\">添加分类</a></div>");
                    builder.Append("<div align=\"left\" style=\"padding:5px;\">");
                    builder.Append("<table border=\"0\" cellpadding=\"2\" cellspacing=\"1\" class=\"TablePanel\">");
                    builder.Append("<tr align=\"center\" class=\"TDHead\">");
                    builder.Append("<td width=\"20%\">分类名称</td><td width=\"20%\">保存目录</td><td width=\"25%\">Meta 关键字</td><td width=\"25%\">Meta 描述</td><td width=\"10%\">&nbsp;</td></tr>");
                    List<Yom.Model.Category> list = new Yom.Data.Category().GetList();
                    foreach (Yom.Model.Category model in list)
                    {
                        builder.Append("<tr><td align=\"center\"><a href=\"javascript:$.Run(4,2," + model.CategoryID.ToString() + ")\">" + model.CategoryName + "</a></td>");
                        builder.Append("<td align=\"center\">" + model.SaveName + "</td>");
                        builder.Append("<td align=\"left\">" + model.SeoKey + "</td>");
                        builder.Append("<td align=\"left\">" + model.SeoDes + "</td>");
                        builder.Append("<td align=\"center\"><img border=\"0\" src=\"Images/Admin/make.png\" alt=\"生成分类HTML\" width=\"16\" height=\"16\" onclick=\"$.Run(4,4," + model.CategoryID.ToString() + ")\" style=\"cursor:pointer\" />&nbsp;<img border=\"0\" src=\"Images/Admin/del.gif\" alt=\"删除分类\" width=\"16\" height=\"16\" onclick=\"$.Run(4,3," + model.CategoryID.ToString() + ")\" style=\"cursor:pointer\" /></td></tr>");
                    }
                    builder.Append("</table><br /><div id=\"ShowMsg\" style=\"color:red\"></div>");
                    base.Response.Write(builder.ToString());
                    break;
            }
        }
    }
}